<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use Overtrue\Socialite\SocialiteManager;
use think\Db;
/**
 * 会员中心
 */
class User extends Frontend
{
    protected $layout = '';
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'), '/');
        }

        //监听注册登录退出的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
    }

    /**
     * 会员中心
     */
    public function index()
    {
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $index = $this->request->post('index','');
            $sort = $this->request->post('sort','');
            
            $where = [];
            $orderBy = [];
            $where['status'] = 'normal';
            $where['is_audit'] = 1;
            $where['user_id'] = $this->auth->id;
           if($index){
                switch ($index) {
                    case 'hot':
                        $orderBy['once_clicks'] = 'desc';
                        break;
                    case 'featured':
                        $where['is_featured'] = 1;
                        break;
                }
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            $weigh = '';
            foreach ($orderBy as $key => $value) {
                $weigh .= $key.' '.$value.',';
            }
            $weigh = rtrim($weigh,',');
            $list = Db::name('Document')->where($where)->order($weigh)->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $user = Db::name('user')->where('id',$this->auth->id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$this->auth->id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$this->auth->id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$this->auth->id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        return $this->view->fetch();
    }
    public function favorites(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $user_id = $this->auth->id;
            $list = Db::name('DocumentUserFavorites')->alias('f')->join('wd_document d','d.id=f.doc_id')->where('f.user_id',$user_id)->order('d.id desc')->field('d.*')->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        
        $user = Db::name('user')->where('id',$this->auth->id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$this->auth->id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$this->auth->id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$this->auth->id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        return $this->view->fetch();
    }
    public function adviser(){
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $user_id = $this->auth->id;
            $list = Db::name('DocumentUserFavorites')->alias('f')->join('wd_document d','d.id=f.doc_id')->where('f.user_id',$user_id)->order('d.id desc')->field('d.*')->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        
        $user = Db::name('user')->where('id',$this->auth->id)->find();
        $group = Db::name('UserGroup')->where('id','in',$user['group_id'])->select();
        $this->assign('group',$group);
        $this->assign('user',$user);
        $this->assign('a',Db::name('Document')->where('status','normal')->where('is_audit',1)->where('user_id',$this->auth->id)->count());
        $this->assign('p',Db::name('DocumentUserComments')->where('user_id',$this->auth->id)->count());
        $this->assign('l',Db::name('Document')->where('status','normal')->where('is_ling',1)->where('user_id',$this->auth->id)->count());
        $this->assign('wqurl',Db::name('category')->where('id',21)->value('url'));
        return $this->view->fetch();
    }
    public function info(){
        if($this->request->isAjax()){
            $params = $this->request->post('row/a');
            $area = explode('/',$params['area']);
            $params['district'] = $area[2];
            $params['city'] = $area[1];
            $params['province'] = $area[0];
            if(!empty($params['saidao'])){
                $params['saidao'] = implode(',',array_map(function( $subArray) {
                    return reset( $subArray); // 获取子数组的第一个值
                },  $params['saidao']));

            }
            if(!empty($params['ziyuan'])){
                $params['ziyuan'] = implode(',',array_map(function( $subArray) {
                    return reset( $subArray); // 获取子数组的第一个值
                },  $params['ziyuan']));
            }
            if(!empty($params['xuyao'])){
                $params['xuyao'] = implode(',',array_map(function( $subArray) {
                    return reset( $subArray); // 获取子数组的第一个值
                },  $params['xuyao']));
            }
            $result = model('user')->allowField(true)->save($params,['id'=>$this->auth->id]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error(model('user')->getError());
            }
              
        }
        $user = Db::name('user')->where('id',$this->auth->id)->find();
        $user['area'] = $user['district']?$user['province'].'/'.$user['city'].'/'.$user['district']:"";
        $this->assign('row',$user);
        return $this->view->fetch();
    }
    /**
     * 会员登录
     */
    public function login()
    {
        $url = Session::get('referer');
        $url = $url ? $url : 'index/index';
        if($this->auth->isLogin()){
            $this->redirect($url);
        }else{
            $code = $this->request->param('code','');
            $site = Config::get('site');
            
            if($code){
                $config = [
                    'wechat'=>[
                        'client_id'   => $site['wechat_open_id'],
                        'client_secret'   => $site['wechat_open_secret'],
                        'redirect'=>$this->request->url(true)
                    ]
                  ];
                $socialite = new SocialiteManager($config);
                $user = $socialite->driver('wechat')->user();
                $wduser = Db::name('user')->where('unionid',$user['original']['unionid'])->find();
                if(empty($wduser)){
                    $wduser = model('User');
                    $wduser->nickname = $user['nickname'];
                    $wduser->avatar = $user['avatar'];
                    $wduser->openid = $user['original']['openid'];
                    $wduser->unionid = $user['original']['unionid'];
                    $wduser->username = $user['nickname'];
                    $wduser->save();
                }
                dump($wduser);
                $this->auth->direct($wduser['id']);
                $this->redirect($url);
            }else{
               header('Location:https://open.weixin.qq.com/connect/qrconnect?appid='.$site['wechat_open_id'].'&redirect_uri='.urlencode($this->request->url(true)).'&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect');
               exit();
            }
        }
    }
    /**
     * 退出登录
     */
    public function logout()
    {
        if ($this->request->isPost()) {
            $this->token();
            //退出本站
            $this->auth->logout();
            $this->success(__('Logout successful'), '/index');
        }
        $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
        $html .= "<script>document.forms['logout_submit'].submit();</script>";

        return $html;
    }

}
